package com._public._transitions 
{
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.geom.Point;
	/**
	 * 位图切割
	 * @author Johnny Cheng
	 * @example
	 * new SplitImage([0.5], null, [graphics], bmpp);
	 */
	public class SplitImage 
	{
		/**
		 * Construct
		 * @param   hSplits       水平方向断裂位置（0-1递增），水平断裂点多少等于数组的长度，如：[0.5]表示水平方向从中间开始有一处断裂，又如：[0.2,0.6]表示水平0.2,0.6位置各有一处断裂
		 * @param	vSplits       垂直方向断裂位置（0-1递增），其他同hSplits
		 * @param	graphics      Graphics数组，长度必须为(hSplits.length + 1) * (vSplits.length + 1)
		 * @param	bmp           填充位图
		 * @param	hCorners      水平拐角数，必须大于0
		 * @param	vCorners      垂直拐角数，必须大于0
		 * @param	hCornerOffset 水平拐角最大偏移量
		 * @param	vCornerOffset 垂直拐角最大偏移量
		 */
		public function SplitImage(hSplits:Array, vSplits:Array, graphics:Array, bmp:BitmapData, hCorners:int = 100, vCorners:int = 100, hCornerOffset:Number = 20, vCornerOffset:Number = 20)
		{
			if (hSplits == null) hSplits = [];
			if (vSplits == null) vSplits = [];
			
			hSplits.push(1);
			vSplits.push(1);
			
			var rows:int = vSplits.length; //行数
			var cols:int = hSplits.length; //列数
			
			var hGap:Number = bmp.width / hCorners;  //水平方向拐角间隔
			var vGap:Number = bmp.height / vCorners; //垂直方向拐角间隔
			
			//创建各个象限绘制点二维数组
			var arr:Array = [];
			for (var i:int = 0; i < rows; i++) {
				arr[i] = [];
				for (var j:int = 0; j < cols; j++) {
					arr[i][j] = [];
					
					// 上边缘
					if (i == 0) arr[i][j][0] = [new Point(j == 0 ? 0 : hSplits[j - 1] * bmp.width, 0), new Point(hSplits[j] * bmp.width, 0)];
					else {
						var temp:Array = arr[i - 1][j][2].concat();
						arr[i][j][0] = temp.reverse();
					}
					
					// 右边缘
					if (j == cols - 1) arr[i][j][1] = [new Point(bmp.width, i == 0 ? 0 : vSplits[i - 1] * bmp.height), new Point(bmp.width, vSplits[i] * bmp.height)];
					else {
						var corners:int = Math.floor((i == 0 ? vSplits[0] : vSplits[i] - vSplits[i - 1]) * vCorners);
						var startX:Number = hSplits[j] * bmp.width;
						var startY:Number = i == 0 ? 0 : vSplits[i - 1] * bmp.height;
						arr[i][j][1] = [];
						for (var m:int = 0; m < corners; m++) {
							arr[i][j][1][m] = new Point(startX + Math.random() * vCornerOffset * 2 - vCornerOffset, startY + m * vGap);
						}
					}
					
					// 下边缘
					if (i == rows - 1) arr[i][j][2] = [new Point(hSplits[j] * bmp.width, bmp.height), new Point(j == 0 ? 0 : hSplits[j - 1] * bmp.width, bmp.height)];
					else {
						corners = Math.floor((j == 0 ? hSplits[0] : hSplits[j] - hSplits[j - 1]) * hCorners);
						startX = hSplits[j] * bmp.width;
						startY = vSplits[i] * bmp.height;
						arr[i][j][2] = [];
						for (m = 0; m < corners; m++) {
							arr[i][j][2][m] = new Point(startX - m * hGap, startY + Math.random() * hCornerOffset * 2 - hCornerOffset);
						}
					}
					
					// 左边缘
					if (j == 0) arr[i][j][3] = [new Point(0, vSplits[i] * bmp.height), new Point(0, i == 0 ? 0 : vSplits[i - 1] * bmp.height)];
					else {
						temp = arr[i][j - 1][1].concat();
						arr[i][j][3] = temp.reverse();
					}
				}
			}
			
			//绘制到graphics中
			var n:int = 0;
			for (i = 0; i < rows; i++) {
				for (j = 0; j < cols; j++) {
					var g:Graphics = graphics[n++];
					g.beginBitmapFill(bmp);
					var points:Array = arr[i][j][0].concat(arr[i][j][1], arr[i][j][2], arr[i][j][3]);
					var pt0:Point = points[0];
					g.moveTo(pt0.x, pt0.y);
					for (m = 0; m < points.length; m++) {
						g.lineTo(points[m].x, points[m].y);
					}
					g.endFill();
				}
			}
		}
		
	}

}